<?php

require_once("User.php");
require_once("UserPost.php");
require_once("UserComment.php");

class DALBase {
	
	private $db;
	private $dbHost = "localhost";
	private $dbUsername = "root";
	private $dbPassword = "";
	private $dbName = "blogg";
	private $dbPort = 3306;
	
	public function __construct() {
		$this->db = new mysqli($this->dbHost, $this->dbUsername, $this->dbPassword, $this->dbName, $this->dbPort);
		
		if ($this->db->connect_errno > 0) {
			die("Unable to connect to database [$db->connect_error]");
		}
	}
	
	public function clearResult() {
		while ($this->db->next_result() && $this->db->more_results()) {
			//free each result.
			$result = $this->db->use_result();
			if ($result instanceof mysqli_result) {
				$result->free();
			}
		}
	}
	
	public function getAllUsers() {
		if (!$result = $this->db->query("CALL getAllUsers()")) {
			 throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
		
		while($object = $result->fetch_assoc()){
		    $ret[] = new User($object["id"],
							  $object["username"],
							  $object["password"],
							  $object["picture"],
							  $object["expireDate"]);
		}
		
		$this->clearResult();		
		return $ret;
	}
	
	public function getUser($username) {
		if (!$result = $this->db->query("CALL getUser('$username')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
		
		$object = $result->fetch_assoc();
		$ret = new User($object["id"],
						$object["username"],
						$object["password"],
						$object["picture"],
						$object["expireDate"]);

		$this->clearResult();		
		return $ret;
	}
	
	public function createUser($username, $password) {
		if (!$this->db->query("CALL createUser('$username', '$password')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
	
	public function updateUserExpireDate($id, $expireDate) {
		if (!$result = $this->db->query("CALL updateUserExpireDate('$id', '$expireDate')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
	
	//------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
	
	public function getPostsOnId($id) {
		if (!$result = $this->db->query("CALL getPostsOnId('$id')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
		
		while($object = $result->fetch_assoc()){
		    $ret[] = new UserPost($object["id"],
							   	  $object["usrId"],
							   	  $object["username"],
							  	  $object["imageString"],
						      	  $object["comment"],
							   	  $object["dateString"]);
		}
		
		$this->clearResult();		
		return $ret;
	}
	
	public function addPost($userId, $imageString, $comment) {
		if (!$this->db->query("CALL addPost('$userId', '$imageString', '$comment')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
	
	public function deletePost($id) {
		if (!$this->db->multi_query("CALL deletePost('$id', @oldImageString);SELECT @oldImageString as oldImageString")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
		
		$this->db->next_result();
		$result = $this->db->store_result();
		$str = $result->fetch_object()->oldImageString;
		$result->free();
		return $str;
	}
	
	public function editPost($id, $imageString, $comment) {
		if (!$this->db->multi_query("CALL editPost('$id', '$imageString', '$comment', @oldImageString);SELECT @oldImageString as oldImageString")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
		
		$this->db->next_result();
		$result = $this->db->store_result();
		$str = $result->fetch_object()->oldImageString;
		$result->free();
		return $str;
	}
	
	public function getCommentsOnId($postId) {
		if (!$result = $this->db->query("CALL getCommentsOnId('$postId')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}

		while($object = $result->fetch_assoc()){
		    $ret[] = new UserComment($object["id"],
							   	  	 $object["usrId"],
							   	  	 $object["postId"],
							   	  	 $object["username"],
						      	  	 $object["comment"],
							   	  	 $object["dateString"]);
		}
		
		$this->clearResult();
		return $ret;
	}
	
	public function addComment($userId, $postId, $comment) {
		if (!$this->db->query("CALL addComment('$userId', '$postId' ,'$comment')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
	
	public function deleteComment($id) {
		if (!$this->db->query("CALL deleteComment('$id')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
	
	public function editComment($id, $comment) {
		if (!$this->db->query("CALL editComment('$id', '$comment')")) {
			throw new \Exception("CALL failed: (" . $this->db->errno . ") " . $this->db->error);
		}
	}
}